// sin信号信息
import 'dart:math';

// const sinSignalCenter = 1.6; // 信号中心电平1.6V
const sinSignalCenter = 0.0; // 信号中心电平1.6V
const sinSignalAmplitude = 1.5; // 信号振幅1.5V
const sinSignalPeriod = 0.5; // 信号周期，秒
const sinStartPhase = pi / 4; // 初始相位
var sinSignalFreq = 1 / sinSignalPeriod; // 信号周期，Hz
const sinSampleRate = 500.0; // 每秒500次采样

class SinSignal {
  double period; // 周期
  double amplitude; // 幅度
  late double frequency; // 频率
  double center; // 0点电平
  double currentphase; // 起始相位
  double sr; // 采样率
  late double step; // 步进长度
  SinSignal(
      {this.period = sinSignalPeriod,
      this.amplitude = sinSignalAmplitude,
      this.center = sinSignalCenter,
      this.currentphase = sinStartPhase,
      this.sr = sinSampleRate}) {
    frequency = 1 / period;
    step = 2 * pi * sinSignalFreq / sr;
  }

  double sample() {
    var v = sin(currentphase) * amplitude + center;
    currentphase += step;
    if (currentphase > 2 * pi) {
      currentphase -= 2 * pi;
    }
    return v;
  }

  List<double> sampleMany(int n) {
    List<double> vs = [];
    for (int i = 0; i < n; i++) {
      vs.add(sample());
    }
    return vs;
  }

  double rate() {
    return sr;
  }
}
